package chapter03

/**
 * @program lu-scala01
 * @description: 伴生对象 class & object
 * @author: zhanglu
 * @create: 2022-03-20 21:17:00
 */
object C11_Object {

  def main(args: Array[String]): Unit = {
//    val errorObj = new Student11("lu", 18)
    val student1 = Student11.newStudent11("lu1", 18);
    student1.printInfo()
    val student2 = Student11.apply("lu2", 18);
    student2.printInfo()
    val student3 = Student11("lu3", 18);
    student3.printInfo()

  }

}

class Student11 private(val name: String, val age: Int) {

  def printInfo() {
    println(s"student name $name age $age sex ${Student11.sex}")
  }

}

/**
 * 伴生对象可以之间访问伴生类的私有属性和方法
 */
object Student11 {
  val sex: String = "男"
  //定义一个类的对象实例的创建方法
  def newStudent11(name: String, age: Int): Student11 = new Student11(name, age)

  /**
   * 调用apply() 方法可以直接省略名称
   */
  def apply(name: String, age: Int): Student11 = new Student11(name, age)

}
